package student.aistar.day11;

import student.aistar.day10.Book;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        Comparator<Book> instance = ComparatorFactory.getInstance(ComparatorFactory.PRICE);

        //1.添加元素的注意点
        //假设排序的规则一旦制定好, 比如price进行排序
        //被添加到容器的这些对象的prices就不能一样
        //如果一旦存在price相同的对象, 那么就会拒绝添加

        //实际的开发中就推荐使用HashSet集合
        //HashSet - 放入到ArrayList集合
        TreeSet<Book> books = new TreeSet<>(instance);
        Book b1 = new Book(1,"1001","java",100.0d);
        Book b2 = new Book(2,"1002","java",200.0d);
        Book b3 = new Book(3,"1003","java",200.0d);
        Book b4 = new Book(2,"1004","python",300.0d);

        books.add(b1);
        books.add(b2);
        books.add(b3);
        books.add(b4);

        books.forEach(a -> System.out.println(a));
    }
}
